﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EmployeeApp.UnitTest.EmployeeService;
using System.Collections.Generic;
using OfficeOpenXml;
using System.Configuration;
using System.IO;

namespace EmployeeApp.UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void checkEmployeeTestCase()
        { 
            EmployeeServiceClient ESC = new EmployeeServiceClient();
            EmployeeServiceClient ESCS = new EmployeeServiceClient();

            EmployeeDto employeeDToA = new EmployeeDto();
            GetAllRequestDto employeeDToS = new GetAllRequestDto();
            EmployeeDto employeeDToU = new EmployeeDto();
            EmployeeDto employeeDToD = new EmployeeDto();

            var ep = new ExcelPackage(new FileInfo(ConfigurationManager.AppSettings["DataTest"]));
            ExcelWorksheet ws = ep.Workbook.Worksheets[ConfigurationManager.AppSettings["WorkSheet"]];

            // first row is column name, array indexer goes from 1 
            // second row is column data, array index goes from 2
            for (int i = 2; i <= ws.Dimension.End.Row; i++)
            {
                if (ws.Cells[i, 1].Value.ToString() == "Insert")
                {
                    // Arrange 
                    employeeDToA.Address = ws.Cells[i, 3].Value.ToString();
                    employeeDToA.EmpId = ws.Cells[i, 4].Value.ToString();
                    employeeDToA.JobTitle = int.Parse(ws.Cells[i, 5].Value.ToString());
                    employeeDToA.MarriedStatus = int.Parse(ws.Cells[i, 6].Value.ToString());
                    employeeDToA.Name = ws.Cells[i, 7].Value.ToString();
                    employeeDToA.Status = int.Parse(ws.Cells[i, 8].Value.ToString());
                    employeeDToA.Surname = ws.Cells[i, 9].Value.ToString();
                    employeeDToA.Title = int.Parse(ws.Cells[i, 10].Value.ToString());
                    employeeDToA.BirthDate = DateTime.Parse(ws.Cells[i, 11].Value.ToString().Replace("'",""));

                    // Act
                    var result = ESC.Add(new AddRequest(employeeDToA));

                    // Assert
                    if (bool.Parse(ws.Cells[i, 14].Value.ToString()))
                        Assert.IsTrue(result.AddResult.Status, ws.Cells[i, 15].Value.ToString());
                    else
                        Assert.IsFalse(result.AddResult.Status, ws.Cells[i, 15].Value.ToString());
                }
                else if (ws.Cells[i, 1].Value.ToString() == "Search")
                {
                    // Arrange 
                    employeeDToS.EmpId = ws.Cells[i, 4].Value.ToString();
                    employeeDToS.Name = ws.Cells[i, 7].Value.ToString();
                    employeeDToS.Status = int.Parse(ws.Cells[i, 8].Value.ToString());
                    employeeDToS.Sort = ws.Cells[i, 12].Value.ToString();
                    employeeDToS.SortType = ws.Cells[i, 13].Value.ToString();

                    // Act
                    var result = ESC.GetAll(new GetAllRequest(employeeDToS));

                    // Assert
                    if (bool.Parse(ws.Cells[i, 14].Value.ToString()))
                        Assert.IsTrue(result.GetAllResult.Status, ws.Cells[i, 15].Value.ToString());
                    else
                        Assert.IsFalse(result.GetAllResult.Status, ws.Cells[i, 15].Value.ToString());
                }
                else if (ws.Cells[i, 1].Value.ToString() == "Update")
                {
                    var resultS = ESCS.GetAll(new GetAllRequest(employeeDToS));

                    // Arrange 
                    employeeDToU.Id = resultS.GetAllResult.Data[int.Parse(ws.Cells[i, 2].Value.ToString())].Id;
                    employeeDToU.Address = ws.Cells[i, 3].Value.ToString();
                    employeeDToU.EmpId = ws.Cells[i, 4].Value.ToString();
                    employeeDToU.JobTitle = int.Parse(ws.Cells[i, 5].Value.ToString());
                    employeeDToU.MarriedStatus = int.Parse(ws.Cells[i, 6].Value.ToString());
                    employeeDToU.Name = ws.Cells[i, 7].Value.ToString();
                    employeeDToU.Status = int.Parse(ws.Cells[i, 8].Value.ToString());
                    employeeDToU.Surname = ws.Cells[i, 9].Value.ToString();
                    employeeDToU.Title = int.Parse(ws.Cells[i, 10].Value.ToString());
                    employeeDToA.BirthDate = DateTime.Parse(ws.Cells[i, 11].Value.ToString().Replace("'", ""));

                    // Act
                    var result = ESC.Update(new UpdateRequest(employeeDToU));

                    // Assert
                    if (bool.Parse(ws.Cells[i, 14].Value.ToString()))
                        Assert.IsTrue(result.UpdateResult.Status, ws.Cells[i, 15].Value.ToString());
                    else
                        Assert.IsFalse(result.UpdateResult.Status, ws.Cells[i, 15].Value.ToString());
                }
                else if (ws.Cells[i, 1].Value.ToString() == "Delete")
                {
                    var resultS = ESCS.GetAll(new GetAllRequest(employeeDToS));

                    // Arrange 
                    List<string> IdList = new List<string>();

                    IdList.Add(resultS.GetAllResult.Data[int.Parse(ws.Cells[i, 2].Value.ToString())].Id.ToString());

                    // Act
                    var result = ESC.Delete(new DeleteRequest(IdList));

                    // Assert
                    if (bool.Parse(ws.Cells[i, 14].Value.ToString()))
                        Assert.IsTrue(result.DeleteResult.Status, ws.Cells[i, 15].Value.ToString());
                    else
                        Assert.IsFalse(result.DeleteResult.Status, ws.Cells[i, 15].Value.ToString());
                }
            }   
        }
    }
}
